<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/1/7
 * Time: 上午12:21
 */

//手机验证码

function post($curlPost,$url){
    //$url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    $return_str = curl_exec($curl);
    curl_close($curl);
    return $return_str;
}


function xml_to_array($xml){
    $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
    if(preg_match_all($reg, $xml, $matches)){
        $count = count($matches[0]);
        for($i = 0; $i < $count; $i++){
            $subxml= $matches[2][$i];
            $key = $matches[1][$i];
            if(preg_match( $reg, $subxml )){
                $arr[$key] = xml_to_array( $subxml );
            }else{
                $arr[$key] = $subxml;
            }
        }
    }
    return $arr;
}

//判断登陆状态
function loginStatus(){
    if (isset($_SESSION['loginUser'])) {

    } else {
        $loginurl = U('Login/index');
        //dump($loginurl);
        echo "<script>alert('请登录系统！');window.location.href='{$loginurl}' </script>";

    }
}

//
function loginStatusAjax(){
    if (isset($_SESSION['loginUser'])) {

    } else {
        $loginurl = U('Login/index');
        //dump($loginurl);
        //$this->error("请登陆系统",$loginurl);
        exit;
    }
}

//分享页面
function sharePage(){

    $jssdk = new \com\JSSDK('wxe711347b63cb2122', 'd0e04e4378da1ca16ae6946ca0641633');
    $signPackage = $jssdk->GetSignPackage();

    $result['sign'] = $signPackage;
    //获取URL
    $url = $_SERVER["REQUEST_URI"];
    $url_arr = explode("/",$url);
    if( "fetcher" == $url_arr[2] && "index" == $url_arr[3] ){
        //接活首页
        $result['title'] = "我要接活";
        $result['url'] = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $result['content'] = '寻找合适的影视圈就业机会';

        return $result;

    }elseif( "task" == $url_arr[2] && "index" == $url_arr[3] && "mission" == $url_arr[4] ){
        //任务主页
        $mission_id = intval($url_arr[5]);
        $mission_db = M('Mission');
        $mission_result = $mission_db->getByMission_id($mission_id);
        if(is_array($mission_result)){
            $content = $mission_result['description'];
            if(strlen($content) > 100){
                $content=str_replace(PHP_EOL,'',getstr($content,98)."...");
            }

            $result['title'] =$mission_result['mission_name'];
            $result['content'] = $content;
            $result['url'] = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        }else{
            $result['title'] = "任务主页";
            $result['content'] = "寻找合适的影视圈就业机会";
            $result['url'] = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        }

        return $result;

    }elseif( "publisher" == $url_arr[2] && "index" == $url_arr[3] ){
        //发布首页
        $result['title'] = "我要发活";
        $result['url'] = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $result['content'] = '寻找合适的影视圈就业机会';

        return $result;

    }elseif( "fetcher_info" == $url_arr[2] && "publishprofile" == $url_arr[3] ){
        //个人主页
        $user_id = intval($url_arr[5]);
        $user_db = M('user');
        $user = $user_db->getByUser_id($user_id);

        $result['title'] = "活控名片-".$user['user_name'].',我在寻找影视圈合适的工作机会。';
        $result['url'] = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $result['content'] = '活控一下，一拍即合！';

        return $result;

    }elseif( "fetcher_info" == $url_arr[2] && "index" == $url_arr[3] ){
        //个人主页
        $user_id = intval($url_arr[5]);
        $user_db = M('user');
        $user = $user_db->getByUser_id($user_id);

        $result['title'] = "活控名片-".$user['user_name'].',我在寻找影视圈合适的工作机会。';
        $result['url'] = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $result['content'] = '活控一下，一拍即合！';

        return $result;

    }else{
        //其他页面
        $result['title'] = "影视圈工作机会-活控";
        $result['url'] = 'http://'.$_SERVER['HTTP_HOST'].U("Login/index");
        $result['content'] = "寻找合适的影视圈就业机会";

        return $result;

    }
}

function getKeywords(){
    $keywords_db = M('keywords');
    $keywords_list = $keywords_db->field(array('keyword'))->order('id asc,keywords.order desc')->limit(5)->select();
    $keywords = array();
    for($i=0;$i<sizeof($keywords_list);$i++){
        $keywords[] = $keywords_list[$i]['keyword'];
    }
    return $keywords;
}
function getstr($string, $length, $encoding  = 'utf-8') {
    $string = trim($string);

    if($length && strlen($string) > $length) {
        //截断字符
        $wordscut = '';
        if(strtolower($encoding) == 'utf-8') {
            //utf8编码
            $n = 0;
            $tn = 0;
            $noc = 0;
            while ($n < strlen($string)) {
                $t = ord($string[$n]);
                if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
                    $tn = 1;
                    $n++;
                    $noc++;
                } elseif(194 <= $t && $t <= 223) {
                    $tn = 2;
                    $n += 2;
                    $noc += 2;
                } elseif(224 <= $t && $t < 239) {
                    $tn = 3;
                    $n += 3;
                    $noc += 2;
                } elseif(240 <= $t && $t <= 247) {
                    $tn = 4;
                    $n += 4;
                    $noc += 2;
                } elseif(248 <= $t && $t <= 251) {
                    $tn = 5;
                    $n += 5;
                    $noc += 2;
                } elseif($t == 252 || $t == 253) {
                    $tn = 6;
                    $n += 6;
                    $noc += 2;
                } else {
                    $n++;
                }
                if ($noc >= $length) {
                    break;
                }
            }
            if ($noc > $length) {
                $n -= $tn;
            }
            $wordscut = substr($string, 0, $n);
        } else {
            for($i = 0; $i < $length - 1; $i++) {
                if(ord($string[$i]) > 127) {
                    $wordscut .= $string[$i].$string[$i + 1];
                    $i++;
                } else {
                    $wordscut .= $string[$i];
                }
            }
        }
        $string = $wordscut;
    }
    return trim($string);
}
function get_seo_info(){
    $setting_db = M('Setting');
    $title = $setting_db->getByKey('SITE_TITLE');
    if(sizeof($title)==0|| $title==false){
        $seo['title'] = '活控';
    }else{
        $seo['title'] = $title['value'];
    }
    $keyword = $setting_db->getByKey('SITE_KEYWORDS');
    if(sizeof($keyword)==0 || $keyword ==false){
        $seo['keyword'] = '活控';
    }else{
        $seo['keyword'] = $keyword['value'];
    }
    $description = $setting_db->getByKey('SITE_DESCRIPTION');
    if(sizeof($description)==0|| $description==false){
        $seo['description']= '活控';
    }else{
        $seo['description'] = $description['value'];
    }

    return $seo;

}

?>